<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<?php
/*
 *  A Part of snock project
 *   Author king-of-war
 * Last modified 2009-4-20
 *  designed for sort operation
 */
class sort {
	private $root_path;
	private $error;
	function __construct($url) {
		/*
		* load connection class
				 */
		include_once $url . 'includes/conn.php';
		/*
		 * load products_sort entity class
		 */
		include_once $url . 'includes/entity/pro_sort.entity.php';
		/*
		* load error report class
		*/
		include_once $url . 'admin/class/error.class.php';
		$error = new Errmsg();
		$this->set_error($error);
		$this->set_root_path($url);
	}
	/*
	 * function : sort_add
	 *  for sort add & update
	 * $fun: 1 means add sort
	 * $fun: 0 means update sort
	 */
	function sort_add($id, $name, $intro, $fun) {
		$conn = new conn();
		if ($fun) {
			/*
			 * sort add codes here
			 */
			$sql = sprintf("insert into products_sort(id,name,intro)values(null,'%s','%s'); ", $name, $intro);
		} else {
			/*
			 * sort update codes here
			 */
			$sql = sprintf("update products_sort set name='%s',intro='%s' where id='%d'", $name, $intro, $id);
		}
		$conn->set_sql($sql);
		$line = $conn->get_line();
		return $line;
		exit;
	}
	/*
	 * function: sort_del
	 * for sort delete
	 * delete sort by id
	 */
	function sort_del($ids) {
		if (isset ($ids)) {
			if (is_array($ids)) {
				$max = count($ids);
				for ($i = 0; $i < $max; $i++) {
					$conn = new conn();
					$sql = sprintf("delete from products_sort where id=%d ; ", $ids[$i]);
					$conn->set_sql($sql);
					$line1 = $conn->get_line();
					$line += $line1;
				}
			} else {
				$conn = new conn();
				$sql = sprintf("delete from products_sort where id=%d", $ids);
				$conn->set_sql($sql);
				$line = $conn->get_line();
			}
		} else {
			echo 'at least choose one for process please';
			$line = 0;
		}
		return $line;
	}
	/*
	 * function sort_read
	 * for sort read
	 * get all sort contants
	 * as type id ,name,intro
	 * $id
	 */
	function sort_read($id) {
		$conn = new conn();
		if (is_array($id)) {
			/*
			 * return the resluts as array
			 * $a means start   $b means counts
			 */
			$a = $id[0];
			$b = $id[1];
			$sql = sprintf("select * from products_sort limit %d,%d ", $a, $b);
		} else {
			/*
			 *  reslut is only one
			 */
			$sql = sprintf("select * from products_sort where id='%d' ", $id);
		}
		$conn->set_sql($sql);
		$line = $conn->get_line();
		if ($line) {
			$result = $conn->get_result();
			for ($i = 0; $row = $result->fetch_assoc(); $i++) {
				$sorts[$i] = new pro_sort_entity();
				$id = $row['id'];
				$name = $row['name'];
				$intro = $row['intro'];
				$sorts[$i]->set_id($id);
				$sorts[$i]->set_name($name);
				$sorts[$i]->set_intro($intro);
			}
		}
		return $sorts;
	}
	/*
	 * function for vars which used above
	 */
	function set_root_path($url) {
		$this->root_path = $url;
	}
	function get_root_path() {
		return $this->root_path;
	}
	function set_error($error) {
		$this->error = $error;
	}
	function get_error() {
		return $this->error;
	}
}
?>
